<template>
	<div class="system-role-container layout-padding">
		<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
			<el-form ref="queryForm" :inline="true">
				<el-form-item label="职位名称">
					<el-input v-model="state.tableParams.name" placeholder="职位名称" clearable />
				</el-form-item>
				<el-form-item label="职位编码">
					<el-input v-model="state.tableParams.code" placeholder="职位编码" clearable />
				</el-form-item>
				<el-form-item>
					<el-button-group>
						<el-button type="primary" icon="ele-Search" @click="handleQuery"> 查询 </el-button>
						<el-button icon="ele-Refresh" @click="resetQuery"> 重置 </el-button>
					</el-button-group>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" icon="ele-Plus" @click="openAddPos"> 新增 </el-button>
				</el-form-item>
			</el-form>

		</el-card>

		<el-card class="full-table" shadow="hover" style="margin-top: 8px">
			<el-table :data="state.posData" style="width: 100%" v-loading="state.loading" border>
				<el-table-column type="index" label="序号" width="70" align="center" />
				<el-table-column prop="name" label="职位名称" align="center" show-overflow-tooltip />
				<el-table-column prop="code" label="职位编码" align="center" show-overflow-tooltip />
				<el-table-column prop="orderNo" label="排序" width="70" align="center" show-overflow-tooltip />
				<el-table-column label="状态" width="70" align="center" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="info" v-if="scope.row.status==2">禁用</el-tag>
						<el-tag type="success" v-else>启用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="createTime" label="修改时间" align="center" show-overflow-tooltip />
				<el-table-column prop="remarks" label="备注" header-align="center" show-overflow-tooltip />
				<el-table-column label="操作" width="140" fixed="right" align="center" show-overflow-tooltip>
					<template #default="scope">
						<el-button icon="ele-Edit" size="small" text type="primary" @click="openEditPos('edit',scope.row)"> 编辑 </el-button>
						<el-button icon="ele-Delete" size="small" text type="danger" @click="onRowDel(scope.row)"> 删除 </el-button>
					</template>
				</el-table-column>
			</el-table>

			<el-pagination
				v-model:currentPage="state.tableParams.pageIndex"
				v-model:page-size="state.tableParams.pageSize"
				:total="state.tableParams.total"
				:page-sizes="[10, 20, 50, 100]"
				small
				background
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				layout="total, sizes, prev, pager, next, jumper"
			/>

		</el-card>
		<PosDialog ref="posDialogRef" :title="state.editPosTitle" @handleQuery="handleQuery" />
	</div>
</template>

<script setup lang="ts" name="systemPos">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { usePosApi } from '/@/api/pos';

// 引入组件
const PosDialog = defineAsyncComponent(() => import('/@/views/system/pos/dialog.vue'));

// 定义变量内容
const posDialogRef = ref();
const state = reactive({
	posData: [] as Array<PosForm>,
	total: 0,
	loading: false,
	// queryParams: {
	// },
	tableParams: {
		name: undefined,
		code: undefined,
		pageIndex: 1,
		pageSize: 20,
		total: 0 as any,
	},
	editPosTitle: '',
});

onMounted(async () => {
	handleQuery();
});

// 查询操作
const handleQuery = async () => {
	state.loading = true;
	//let params = Object.assign(state.tableData.param);
	await usePosApi().posPageList(state.tableParams).then(async (res: AjaxResult)=>{
		const _data : Array<PosForm> = res.data.items;
		//console.log('ok-res:'+JSON.stringify(res.data));
		state.posData = _data; //JSON.parse(JSON.stringify(res.data));
		state.tableParams.total = res.data?.total;

		setTimeout(() => {
			state.loading = false;
		}, 500);
	});


	// state.loading = true;
	// let params = Object.assign(state.queryParams, state.tableParams);
	// var res = await getAPI(SysRoleApi).apiSysRolePagePost(params);
	// state.roleData = res.data.result?.items ?? [];
	// state.tableParams.total = res.data.result?.total;
	// setTimeout(() => {
	// 	state.loading = false;
	// }, 500);
};

// 重置操作
const resetQuery = () => {
	state.tableParams.name = '';
	state.tableParams.code = '';
	handleQuery();
};

// 打开新增页面
const openAddPos = async(type:string) => {
	state.editPosTitle = '添加职位';
	const _row : PosForm = {status: 0, orderNo: 100 }
	posDialogRef.value.openDialog(type, _row);
};

// 打开编辑页面
const openEditPos = async (type: string, row: Object) => {
	state.editPosTitle = '编辑职位';
	posDialogRef.value?.openDialog(type, row);
};


// 删除角色
const onRowDel = (row: PosForm) => {
	ElMessageBox.confirm(`此操作将永久删除职位名称：“${row.name}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
	.then(async () => {
		await usePosApi().deletePos({ id: row.id }).then(async (res:AjaxResult) => {
			ElMessage.success('删除成功');
			handleQuery();
		});
	})
	.catch(() => {});
};
// 改变页面容量
const handleSizeChange = (val: number) => {
	state.tableParams.pageSize = val;
	handleQuery();
};

// 改变页码序号
const handleCurrentChange = (val: number) => {
	state.tableParams.pageIndex = val;
	handleQuery();
};

</script>

<style scoped lang="scss">
.system-role-container {
	.system-role-padding {
		padding: 15px;
		.el-table {
			flex: 1;
		}
	}
}
</style>
